#ifndef INFO_H
#define INFO_H

#include <iostream>
#include <cstring>
using namespace std;

// 定义一个学生结构体节点
struct student_node
{
    string student_id;
    string student_name;
    string student_gender; // male and female
    int exam_grade;
    int rank = 0;
    student_node *next;
};

class studentlist
{
private:
    student_node *head;
    student_node *tail;
    int size;

public:
    studentlist()
    {
        size = 0;
        head = new student_node;
        head->exam_grade = 0;

        tail = new student_node;
        tail->exam_grade = -1;
        head->next = tail;
        tail->next = nullptr;
    }

    void add_student(student_node *student)
    {
        if (head->next == tail)
        {
            head->next = student;
            student->next = tail;
            student->rank = 1;
            size++;
        }
        else
        {
            // 按照成绩大小建立链表,成绩一样的时候按照学号大小，小的放前面
            student_node *current = head;
            while (current->next->exam_grade > student->exam_grade)
            {
                current = current->next;
            }
            if (current->next->exam_grade == student->exam_grade)
            {
                if (current->next->student_id > student->student_id)
                {
                    student->next = current->next;
                    current->next = student;
                    // 插到前面
                }
                else
                {
                    current = current->next;
                    student->next = current->next;
                    current->next = student;
                    // 插到后面
                }
            }
            else
            {
                // 找到不大于待插入学生成绩的第一个学生节点
                student->next = current->next;
                current->next = student;
            }
            // 插入学生节点
            size++;
        }
    }

    void del_student(int rank)
    {

        student_node *current = head;
        while (current->next->rank != rank)
        {
            current = current->next;
        }
        // 找到待删的学生节点的前一个

        // 再找待删距离待删除节点最近的后一个
        student_node *latter = current->next; // 现在是第一个待删除节点
        while (latter->next->rank == latter->rank)
        {
            latter = latter->next;
        }
        current->next = latter->next;
        // 跳过中间的部分
    }

    void print_list()
    {
        student_node *current = head->next;
        while (current != tail)
        {
            cout << current->student_id << ' ' << current->student_name << ' ' << current->student_gender << ' ' << current->exam_grade << endl;
            current = current->next;
        }
    }

    // 给建完的链表给排名
    void give_rank()
    {
        int sequence = 1;
        student_node *current = head;
        while (current->next != tail)
        {
            if (current == head)
            {
                current->next->rank = 1;
                current = current->next;
                sequence++;
                continue;
            }

            if (current->next->exam_grade == current->exam_grade)
            {
                current->next->rank = current->rank;
            }
            else
            {
                current->next->rank = sequence;
            }

            current = current->next;
            sequence++;
        }
    }
};
#endif